import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient

import urllib
import json
import datetime
import time

from tornado.options import define, options
define("port", default=8001, help="run on the given port", type=int)

def fib(n):
	if n ==0 or n ==1:
		return n
	return fib(n-1) + fib(n -2)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        query = self.get_argument('q')
        print query
        client = tornado.httpclient.HTTPClient()
        response = client.fetch("http://localhost:8008?q=syn" + \
                urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
        #time.sleep(30)
        #print fib(30)
        self.write("""
<div style="text-align: center">
    <div style="font-size: 72px">%s</div>
    <div style="font-size: 144px">%.02f</div>
    <div style="font-size: 24px">tweets per second</div>
</div>""")

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()